package com.elite.groovy.dev;

import com.alibaba.fastjson.JSONObject;
import com.elite.groovy.kh.TxConfigDev;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.dev
 * @className: devHoliday
 * @author: LiJT
 * @description: TODO
 * @date: 2024-06-12 18:24
 * @version: 1.0
 */
public class getHumanRules {
    private static final Log log = LogFactory.getLog(getHumanRules.class);

    public static void main(String[] args) throws Exception {

        JdbcTemplate jt = TxConfigDev.getJdbcTemplate();
        dealHumanRule(jt, null,"");
    }

    // 获取规则在线人
    public static JSONObject dealHumanRule(JdbcTemplate jt, List<Map<String, Object>> resRule_List, String old_StaffID) {
        StringBuffer to_Queue_Info = new StringBuffer();
        JSONObject res_HumanJson = new JSONObject();
        log.debug("dealHumanRule resRule_List:" + resRule_List + " old_StaffID:" + old_StaffID);

        String res_Type = "";
        String res_StaffID = "";
        String res_User_Status = "";

        String old_Staff_ID = "";
        String old_User_Status = "";
        boolean is_Exist = false; //原坐席是否存在规则在线坐席中, 默认不存在


        if (resRule_List.size() > 0) {

            // 获取规则中的坐席
            String staffStr_IDs = resRule_List.get(0).get("staffstr_id").toString();
            log.debug("dealHumanRule 读取规则配置全部坐席ID：" + staffStr_IDs);

            // 查找规则内的坐席属于哪个专属队列并且在线
            String checkSQL = "Select q.Id As Queueid, Cca.Id As Staff_Id, User_Status As User_Status From Queue q Left Join Property_Value Qv On Qv.Property_Key = q.Id Left Join Agent_Queue Aq On Aq. Queue_Id = q.Id Left Join Chat_Cache_Agentbyidmap Cca On Cca.Id = Aq.Agent_Id Where Find_In_Set(Cca.Id, ?) And Property_Id = 1035 And Cca.User_Status > 0 And Qv.Value = 1 Order By Find_In_Set(Cca.Id, ?)";
            List<Map<String, Object>> checkSQL_List = jt.queryForList(checkSQL, staffStr_IDs, staffStr_IDs);
            log.debug("dealHumanRule 检查规则配置全部坐席&对应队列是否在线(登录)-结果" + ":" + checkSQL_List.toString());

            to_Queue_Info.append(System.lineSeparator() + "检查规则配置全部坐席&对应队列是否在线(登录)-结果" + ":" + checkSQL_List.toString());

            // 检查是否通过规则找到有在线坐席
            if (checkSQL_List.size() > 0) {

                // 检查1:遍历当前规则在线坐席数组查找原专属坐席是否存在规则内
                for (int i = 0; i < checkSQL_List.size(); i++) {
                    old_Staff_ID = checkSQL_List.get(i).get("Staff_Id").toString();
                    old_User_Status = checkSQL_List.get(i).get("User_Status").toString();
                    is_Exist = old_StaffID.equals(old_Staff_ID);
                    if (is_Exist) {
                        break; //检查到存在，跳出
                    }
                }

                if (!is_Exist) {

                    res_StaffID = checkSQL_List.get(0).get("Staff_Id").toString();
                    res_User_Status = checkSQL_List.get(0).get("User_Status").toString();

                    res_HumanJson.put("res_type", 1);
                    res_HumanJson.put("res_staff_id", res_StaffID);
                    res_HumanJson.put("res_user_status", res_User_Status);
                    res_HumanJson.put("res_msg", "原坐席不在在线坐席清单中，取规则中在线坐席第一个坐席");
                    log.debug("原坐席不在在线坐席清单中，取规则中在线坐席第一个坐席" + ":" + res_StaffID);

                } else {

                    res_HumanJson.put("res_type", 2);
                    res_HumanJson.put("res_staff_id", old_Staff_ID);
                    res_HumanJson.put("res_user_status", old_User_Status);
                    res_HumanJson.put("res_msg", "原坐席在在线坐席清单中，默认优先给原来的坐席");
                    log.debug("原坐席在在线坐席清单中，默认优先给原来的坐席" + ":" + old_StaffID);

                }

            } else {
                res_HumanJson.put("res_type", -1);
                res_HumanJson.put("res_staff_id", "");
                res_HumanJson.put("res_user_status", "");
                res_HumanJson.put("res_msg", "规则中无在线坐席,放弃处理");
            }


        } else {
            res_HumanJson.put("res_type", -1);
            res_HumanJson.put("res_staff_id", "");
            res_HumanJson.put("res_user_status", "");
            res_HumanJson.put("res_msg", "规则中无数据,放弃处理");

        }
        log.debug("获取人工队列结果:" + res_HumanJson.toString());
        to_Queue_Info.append(System.lineSeparator() + "获取人工队列结果:" + res_HumanJson.toString());
        return res_HumanJson;
    }
}
